<?php
class ModelConfigWeixin extends Model {
	public function getAccessToken($appid,$secret) {
		
		if(isset($this->session->data['accessToken'])){
			$cache = $this->session->data['accessToken'];
	
			if ($cache && $cache['expire'] > time()) {
	
				return $cache['acccessToken'];
	
			}
		}
		
		$url = 'https://api.weixin.qq.com/cgi-bin/token';

		$params = array('grant_type' => 'client_credential', 'appid' => $appid, 'secret' => $secret);

		$_data = $this->curlGet($url, $params);

		if ($_data) {

			$data = json_decode($_data, TRUE);

			if (isset($data['errcode'])) {

				die($data['errmsg']);

			}

			$expire = time() + $data['expires_in'] - 200;

			$accessToken = array('acccessToken' => $data['access_token'], 'expire' => $expire);
			
			$this->session->data['accessToken'] = $accessToken;

			return $data['access_token'];

		}

	}
	
	private function curlGet($url, $params = array(), $option = array()) {

		if (!empty($params)) {

			$p = http_build_query($params);



			if (FALSE === strpos('?', $url)) {

				$url = $url . '?' . $p;

			} else {

				$url = $url . '&' . $p;

			}

		}

		$ch = curl_init($url);


		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

		curl_setopt_array($ch, $option);

		$result = curl_exec($ch);

		curl_close($ch);

		return $result ? $result : false;

	}
	
	
	public function getUserList($token,$nextOpenId = NULL) {

		$url = 'https://api.weixin.qq.com/cgi-bin/user/get';

		if ($nextOpenId) {

			$url .= '?next_openid=' . $nextOpenId;

		}

		$json = $this->request($url,$token);

		return json_decode($json, TRUE);

	}
	
	private function request($url, $token,$data = array(), $method = 'get') {

		if (!$token) {

			die("Cannot get accessToken");

		}
		if (FALSE === strpos($url,'?')) {
			$connect = '?';

		} else {

			$connect = '&';

		}

		$url = $url . $connect . 'access_token=' . $token;
		$params = array();

		if ($method == 'post') {

			if (!empty($data))

			$params = $this->unicodeDecode(json_encode($data));

			return $this->curlPost($url, $params);

		} else {

			return $this->curlGet($url, $data);

		}

	}
	
	private function unicodeDecode($string) {

		$matches = NULL;

		preg_match_all("/\\\\u\w{4}/i", $string, $matches);

		if (!empty($matches)) {

			foreach ($matches[0] as $unicode) {

				$code = base_convert(substr($unicode, 2, 2), 16, 10);

				$code2 = base_convert(substr($unicode, 4), 16, 10);

				$c = chr($code) . chr($code2);

				$c = iconv('UCS-2BE', 'UTF-8', $c);

				$string = str_replace($unicode, $c, $string);

			}

		}
		return $string;
	}
	
	private function curlPost($url, $params = NULL, $option = array()) {

		if (is_array($params) && !empty($params)) {

			$p = http_build_query($params);

		} else {

			$p = $params;

		}

		$ch = curl_init($url);

		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

		curl_setopt($ch, CURLOPT_POST, TRUE);

		curl_setopt($ch, CURLOPT_POSTFIELDS, $p);

		curl_setopt_array($ch, $option);

		$result = curl_exec($ch);

		curl_close($ch);

		return $result ? $result : false;

	}
	
	public function getUserInfo($openId,$token) {

		$url = 'https://api.weixin.qq.com/cgi-bin/user/info?openid=' . $openId;
		$json = $this->request($url,$token);

		return json_decode($json, TRUE);

	}
	
	
}
?>